﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Comp_GeoData;

namespace Comp_Geo
{
    public class Graph
    {
        #region Members

        List<Node> nodes;

        #endregion

        #region Properties

        public List<Node> Nodes { get { return nodes; } set { nodes = value; } }

        #endregion

        #region Initilization

        public Graph() { }

        public Graph(List<Node> nodes)
        {
            this.nodes = nodes;
        }

        #endregion

        #region Draw

        public void DrawGraph(GameTime gameTime, SpriteBatch spriteBatch, Texture2D dotTexture)
        {
            LineSegment lineSegment;
            List<LineSegment> lineSegments = new List<LineSegment>();
            foreach (Node node in nodes)
            {
                foreach (Node adjNode in node.AdjacentNodes)
                {
                    lineSegment = new LineSegment(node.Position, adjNode.Position);
                    lineSegments.Add(lineSegment);
                }
            }

            foreach (LineSegment lineSeg in lineSegments)
            {
                Vector2 point1 = lineSeg.StartPoint;
                Vector2 point2 = lineSeg.EndPoint;

                Vector2 dir = point1 - point2;
                float len = dir.Length();
                float theta = (float)Math.Asin(-dir.Y / len);
                Rectangle r = new Rectangle((int)(point1.X), (int)(point1.Y), (int)len, 2);

                spriteBatch.Draw(dotTexture, r, new Rectangle(0, 0, 1, 1), Color.Blue, theta, Vector2.Zero, SpriteEffects.None, 0.0f);

                //float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
                //float length = Vector2.Distance(point1, point2);

                //spriteBatch.Draw(dotTexture, point1, null, Color.Blue, angle, Vector2.Zero, new Vector2(length, 1), SpriteEffects.None, 0);
            }
            #region Old Code
            //foreach (Node node in nodes)
            //{
            //    foreach (Node node2 in node.AdjacentNodes)
            //    {
            //        Vector2 point1 = node.Position;
            //        Vector2 point2 = node2.Position;
            //        //float distance = Vector2.Distance(point1, point2);
            //        //float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            //        //spriteBatch.Draw(dotTexture, point1, null, Color.Blue, angle, Vector2.Zero, new Vector2(distance, 1), SpriteEffects.None, 0);


            //        Vector2 dir = point1 - point2;
            //        float len = dir.Length();
            //        float theta = (float)Math.Asin(-dir.Y / len);
            //        Rectangle r = new Rectangle((int)(point1.X), (int)(point1.Y), (int)len, 2);

            //        spriteBatch.Draw(dotTexture, r, new Rectangle(0, 0, 1, 1), Color.Blue, theta, Vector2.Zero, SpriteEffects.None, 0.0f);
            //    }
            //}
            #endregion
        }

        #endregion
    }

    public class Node
    {
        public Vector2 Position;
        public List<Node> AdjacentNodes;

        public Node() { }

        public Node(Vector2 Position)
        {
            this.Position = Position;
        }

        public Node(Vector2 Position, List<Node> AdjacentNodes)
        {
            this.Position = Position;
            this.AdjacentNodes = AdjacentNodes;
        }

        public int fScore;
        public int hScore;
        public int gScore;

        public void PrintNode(int index, List<Node> adjNode)
        {
            Console.WriteLine("NodeIndex:{0}", index);
            if (AdjacentNodes != null)
            {
                foreach (Node node in adjNode)
                {
                    Console.WriteLine("NodeIndex:{0} contains Node(NodeIndex:{1})", index, adjNode.IndexOf(node));
                }
            }
        }
    }
}
